<template>
  <div>
    <table>
      <tr>
        <th>姓名</th>
        <th>学号</th>
        <th>语文</th>
        <th>数学</th>
        <th>外语</th>
        <th>物理</th>
        <th>化学</th>
        <th>生物</th>
        <th>操作</th>
      </tr>
      <tr class="hover" v-for="item in allData" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.id }}</td>
        <td>{{ item.Language }}</td>
        <td>{{ item.Math }}</td>
        <td>{{ item.English }}</td>
        <td>{{ item.Physics }}</td>
        <td>{{ item.Chemistry }}</td>
        <td>{{ item.Biology }}</td>
        <!-- <td><span @click="change(item.id)" class="delete">添 加</span></td> -->
        <td><el-button type="text" @click="change(item.id)">添 加</el-button></td>
      </tr>
    </table>
    <!-- 弹窗修改成绩 -->
    <el-dialog title="添加成绩" :visible.sync="dialogFormVisible" :center="true">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="语文" :label-width="formLabelWidth" prop="language">
          <el-input v-model.number="form.language" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数学" :label-width="formLabelWidth" prop="math">
          <el-input v-model.number="form.math" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="外语" :label-width="formLabelWidth" prop="english">
          <el-input v-model.number="form.english" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="物理" :label-width="formLabelWidth" prop="physics">
          <el-input v-model.number="form.physics" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="化学" :label-width="formLabelWidth" prop="chemistry">
          <el-input v-model.number="form.chemistry" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="生物" :label-width="formLabelWidth" prop="biology">
          <el-input v-model.number="form.biology" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="resetForm('form')">重 置</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from '@/utlis/axios'

export default {
  data() {
    return {
      allData: null,
      //弹窗样式
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        language: '', //语文成绩
        math: '', //数学成绩
        english: '', //外语成绩
        physics: '', //物理成绩
        chemistry: '', //化学成绩
        biology: '', //生物成绩
      },
      formLabelWidth: '120px',
      //表单验证规则
      rules: {
        language: [
          { required: true, message: '成绩不能为空' },
          { type: 'number', message: '成绩必须为数字值' },
        ],
        math: [
          { required: true, message: '成绩不能为空' },
          { type: 'number', message: '成绩必须为数字值' },
        ],
        english: [
          { required: true, message: '成绩不能为空' },
          { type: 'number', message: '成绩必须为数字值' },
        ],
        physics: [
          { required: true, message: '成绩不能为空' },
          { type: 'number', message: '成绩必须为数字值' },
        ],
        chemistry: [
          { required: true, message: '成绩不能为空' },
          { type: 'number', message: '成绩必须为数字值' },
        ],
        biology: [
          { required: true, message: '成绩不能为空' },
          { type: 'number', message: '成绩必须为数字值' },
        ],
      },
      id: '',
    }
  },
  methods: {
    async getDate() {
      const { data: res } = await axios.get('/grade')
      this.allData = res.data
    },
    change(id) {
      this.dialogFormVisible = true
      this.id = id
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    //提交表单
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.dialogFormVisible = false
          const { data: res } = await axios.post('/updatagrade', { Language: this.form.language, Math: this.form.math, English: this.form.english, Physics: this.form.physics, Chemistry: this.form.chemistry, Biology: this.form.biology, id: this.ID })
          this.getDate()
          this.$message({
            message: '添加成绩成功',
            type: 'success',
          })
          this.resetForm(formName)
        }
      })
    },
  },
  created() {
    this.getDate()
  },
  computed: {
    ID() {
      return this.id
    },
  },
}
</script>

<style lang="less" scoped>
table {
  width: 100%;
  border: 1px solid #ccc;
  border-collapse: collapse;

  th {
    border: 1px solid rgb(165, 14, 14);
  }

  tr,
  td {
    text-align: center;
    border: 1px solid rgb(122, 21, 195);
    height: 80px;

    .delete {
      cursor: pointer;
      color: #409eff;
      font-size: 14px;
      font-family: Arial;
    }
  }

  .hover {
    &:hover {
      background-color: #ccc;
    }

    .color {
      font-size: 20px;
      color: rgba(0, 255, 255, 0.605);
      cursor: pointer;
    }
  }
}
</style>